<?php
/** 
*
* @author Tom Martin (Poppertom69) tom.martin60@btinternet.com
* @package Anonymous MOD
* @version $Id$
* @copyright (c) 2007 Poppertom69
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

function welcome_user($username)
{
	global $config, $user, $db, $phpbb_root_path, $phpEx;

	include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
	
		$backup_user = array(
		'data'	=> $user->data,
		
		);

	$sql = 'SELECT user_id
		FROM ' . USERS_TABLE . "
		WHERE username = '$username'";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	
	$user_id		= $row['user_id'];
	
	$db->sql_freeresult($result);

	// Backup the $user->data array, as we have to modify it
	$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_poster_id'";
			$result = $db->sql_query($sql);
			$post_bot_id =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);


	$sql = 'SELECT username, user_colour
		FROM ' . USERS_TABLE . "
		WHERE user_id = '$post_bot_id'";
	$result	= $db->sql_query($sql);
	$row	= $db->sql_fetchrow($result);
	$db->sql_freeresult($result);

	$user->data = array_merge($user->data, array(
		'user_id'		=> (int) $post_bot_id,
		'username'		=> $row['username'],
		'user_colour'	=> $row['user_colour'],
	));

	$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_forum'";
			$result = $db->sql_query($sql);
			$forum =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_message'";
			$result = $db->sql_query($sql);
			$welcome_message =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'welcome_subject'";
			$result = $db->sql_query($sql);
			$welcome_subject =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);

	// Parse the text with the bbcode parser and write into $text
	$subject	= $welcome_subject;
	$message	= $welcome_message;
	$text		= utf8_normalize_nfc($message);
	

	// Switch array keys, with values in welcome pm.
	$welcome_vars = array(
		'{USERNAME}'		=> $username,
		'{USER_ID}'			=> $user_id,
		'{SITE_NAME}'		=> $config['sitename'],
		'{SITE_DESC}'		=> $config['site_desc'],
		'{ROOT_PATH}'		=> $phpbb_root_path,
	);

	$text = str_replace(array_keys($welcome_vars), array_values($welcome_vars), $text);
	$subject = str_replace(array_keys($welcome_vars), array_values($welcome_vars), $subject);
	
	// variables to hold the parameters for submit_post
	$poll = $uid = $bitfield = $options = ''; 

	generate_text_for_storage($subject, $uid, $bitfield, $options, false, false, false);
	generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);


	$data = array( 
	'forum_id'			=> $forum,
	'icon_id'			=> false,

	'enable_bbcode'		=> true,
	'enable_smilies'	=> true,
	'enable_urls'		=> true,
	'enable_sig'		=> true,

	'message'			=> $text,
	'message_md5'		=> md5($text),
				
	'bbcode_bitfield'	=> $bitfield,
	'bbcode_uid'		=> $uid,

	'post_edit_locked'	=> 0,
	'topic_title'		=> $subject,
	'notify_set'		=> false,
	'notify'			=> false,
	'post_time' 		=> 0,
	'forum_name'		=> '',
	'enable_indexing'	=> true,

	);

	submit_post('post', $subject, '', POST_NORMAL, $poll, $data);

	//Lets do the updating of the posts table
	$sql = 'UPDATE ' . POSTS_TABLE . " SET post_approved = 1 WHERE post_subject = '$subject'";
	$db->sql_query($sql);
	
	//Lets do the updating of the posts table
	$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_approved = 1 WHERE topic_title = '$subject'";
	$db->sql_query($sql);
	
	// Restore the $user->data array
	$user->data	= $backup_user['data'];
	unset($backup_user);
	
	}
	
	
	
	function user_birthday($username, $age)
{
	global $config, $user, $db, $phpbb_root_path, $phpEx;

	include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
	
	$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_subject'";
			$result = $db->sql_query($sql);
			$birthday_subject =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);

	// Parse the text with the bbcode parser and write into $text
	$subject	= $birthday_subject;
	
	// Switch array keys, with values in welcome pm.
	$birthday_vars = array(
		'{USERNAME}'		=> $username,
		'{USER_ID}'			=> $user_id,
		'{AGE}'				=> $age,
		'{SITE_NAME}'		=> $config['sitename'],
		'{SITE_DESC}'		=> $config['site_desc'],
		'{ROOT_PATH}'		=> $phpbb_root_path,
	);
	
		$subject = str_replace(array_keys($birthday_vars), array_values($birthday_vars), $subject);
	
	$sql = 'SELECT *
				FROM ' . POSTS_TABLE . "
				WHERE post_subject = '$subject'";
			$result = $db->sql_query($sql);
			$continue = $db->sql_affectedrows();	
			$db->sql_freeresult($result);
			
			if($continue == 1){ return;
			}
			else
			{
	
	
		$backup_user = array(
		'data'	=> $user->data,
		
		);

	$sql = 'SELECT user_id
		FROM ' . USERS_TABLE . "
		WHERE username = '$username'";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	
	$user_id		= $row['user_id'];
	
	$db->sql_freeresult($result);

	// Backup the $user->data array, as we have to modify it


	$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_poster_id'";
			$result = $db->sql_query($sql);
			$post_bot_id =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);


	$sql = 'SELECT username, user_colour
		FROM ' . USERS_TABLE . "
		WHERE user_id = '$post_bot_id'";
	$result	= $db->sql_query($sql);
	$row	= $db->sql_fetchrow($result);
	$db->sql_freeresult($result);

	$user->data = array_merge($user->data, array(
		'user_id'		=> (int) $post_bot_id,
		'username'		=> $row['username'],
		'user_colour'	=> $row['user_colour'],
	));

	$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_forum'";
			$result = $db->sql_query($sql);
			$forum =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'birthday_message'";
			$result = $db->sql_query($sql);
			$birthday_message =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
			
	$message	= $birthday_message;
	$text		= utf8_normalize_nfc($message);	

	$text = str_replace(array_keys($birthday_vars), array_values($birthday_vars), $text);
	
		// variables to hold the parameters for submit_post
	$poll = $uid = $bitfield = $options = ''; 

	generate_text_for_storage($subject, $uid, $bitfield, $options, false, false, false);
	generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);

	$data = array( 
	'forum_id'			=> $forum,
	'icon_id'			=> false,

	'enable_bbcode'		=> true,
	'enable_smilies'	=> true,
	'enable_urls'		=> true,
	'enable_sig'		=> true,

	'message'			=> $text,
	'message_md5'		=> md5($text),
				
	'bbcode_bitfield'	=> $bitfield,
	'bbcode_uid'		=> $uid,

	'post_edit_locked'	=> 0,
	'topic_title'		=> $subject,
	'notify_set'		=> false,
	'notify'			=> false,
	'post_time' 		=> 0,
	'forum_name'		=> '',
	'enable_indexing'	=> true,

	);

	submit_post('post', $subject, '', POST_NORMAL, $poll, $data);	

	}
	
	// Restore the $user->data array
	$user->data	= $backup_user['data'];
	unset($backup_user);

}
?>
